﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Nizm0.Chat.Client
{

    public partial class frmChatClient : Form
    {
        private Client _client;
        private Messages.User _user;
        private List<Messages.User> _userList = new List<Messages.User>();

        private System.Threading.Thread _th;

        public frmChatClient():this(new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8000))
        {
            //System.Net.IPEndPoint ip = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8000);
        }

        public frmChatClient(System.Net.IPEndPoint ip)
        {
            InitializeComponent();
            _client = new Client(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(textBox2.Text.Trim()), 8010), textBox1.Text);
            _client.OnUserAdd += new Client.UserAddEventHandler(_client_OnUserAdd);
            _client.MessageReceived += new Client.MessageEvenHandler(_client_MessageReceived);
            //System.Security.Principal.WindowsIdentity.GetCurrent().Name
            _user = new Messages.User(textBox1.Text.Trim(), new System.Net.IPEndPoint(System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0], 8010), Messages.User.Status.Available);
            _client.Send(new Messages.LoginMessage(_user));
            _th = new System.Threading.Thread(_client.Recive);
            _th.Start();

        }

        void _client_MessageReceived(Messages.TextMessage message)
        {
            if (richTextBox_Chat.InvokeRequired)
                richTextBox_Chat.Invoke((MethodInvoker)delegate
                {
                    richTextBox_Chat.AppendText(message.Sender.Name + ": " + message.MessageText);
                });
            else
                richTextBox_Chat.AppendText(message.Sender.Name + ": " + message.MessageText);
        }

        private Messages.User _client_OnUserAdd(Messages.LoginMessage message)
        {
            _userList.Add(message.Sender);
            //webBrowser1.DocumentText = "<div>" + "Logged new user: " + message.MessageUser.Name + "<div>";
            //MessageBox.Show("New user: " + message.MessageUser.Name, "New user");
            if (richTextBox_Chat.InvokeRequired)
                richTextBox_Chat.Invoke((MethodInvoker)delegate
                {
                    richTextBox_Chat.AppendText("Login: " + message.Sender.Name);
                });
            else
                richTextBox_Chat.AppendText("Login: " + message.Sender.Name);
            return message.Sender;
        }


        private void toolStripButton_Send_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text.Length > 0)
            {
                //object o = new Message(richTextBox1.Text);
                _client.Send(new Messages.TextMessage(_user, _user, richTextBox1.Text));
                richTextBox1.Clear();
            }
            else
            {
                //webBrowser1.te
            }
        }

        private void frmChatClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            _client.Disconnect();
            _th.Abort();
            foreach (Control con in this.Controls)
            {
                if (con.IsDisposed)
                    con.Dispose();
            }

        }
    }
}
